﻿$.fn.logout = function (callback) {
    $.get("login.ashx?action=logout", function () {
        window.onunload = null;
        window.onbeforeunload = null;
        window.location = "login.aspx";
    });
}

$.fn.contentHeight = function () {
    return $("#ASPxSplitter1_1i1").height();
}

$.fn.outerHTML = function (s) {
    return (s) ? this.before(s).remove() : $('<p>').append(this.eq(0).clone()).html();
}

$.fn.closeTab = function (index) {
    $(this).mouseover(function () { $(this).css("opacity", "1") }).
            mouseout(function () { $(this).css("opacity", "0.5") }).
    click(function () {
        var pageControl = $(this).parents("#ASPxPageControl1");
        var tabContent = pageControl.find("#ASPxPageControl1_C" + index);
        //StdQuery
        var queryName = tabContent.children().contents().find("#hfCurQueryName").val();
        //DataObject
        var objectName = tabContent.children().contents().find("#hfCurObjectName").val();
        //关闭模块
        tabContent.remove();
        var actionTab = pageControl.find("#ASPxPageControl1_T" + index);

        var prevTab = actionTab.prev().prev().prev();
        var tabAT = pageControl.find("#ASPxPageControl1_AT" + index);


        //removesplit
        var width = (actionTab.width() == 0 ? tabAT.width() : actionTab.width()) + tabAT.prev().width();
        tabAT.remove();
        actionTab.prev().remove();
        actionTab.remove();
        prevTab.click();

        var iframeWidth = $("#iframeContent").width();
        var tabTable = pageControl.find("#ASPxPageControl1_TC > table");
        var left = tabTable.css("margin-left").replace("px", "");
        if (left != "0") {
            var marginleft = parseInt(left) + width;
            marginleft = marginleft < 0 ? marginleft : 0;
            tabTable.css("margin-left", marginleft + "px");

            if (marginleft == 0) {
                var imgs = tabTable.parent().children("img");
                imgs.eq(0).css("opacity", "0");
                imgs.eq(1).css("opacity", "0");
            }
        }
        $.get("../HandelModule.ashx?action=close&ModuleID=" + actionTab.attr("moduleid") + "&QueryName=" + queryName + "&ObjectName=" + objectName);
        return false;
    });
}

$.fn.addTab = function (args) {


    var settings = {
        url: "",
        text: "",
        moduleID: ""
    }
    $.extend(settings, args);


    
    var contentWindow = parent.$("#iframeContent")[0].contentWindow;
    var pageControl = contentWindow.ASPxPageControl1;

    var i = pageControl.tabs.length;
    var p = [settings.text, undefined, undefined, undefined];
    var trHeader = contentWindow.$("#ASPxPageControl1_SVA > table > tbody > tr");

    var dxtcTab = trHeader.find("[class*='dxtcTab']").last().clone();
    var dxtcActiveTab = trHeader.find("[class*='dxtcActiveTab']").last().clone();
    dxtcTab.attr("id", "ASPxPageControl1_T" + i);
    dxtcTab.find("td").attr("id", "ASPxPageControl1_T" + i + "T").text(p[0]);
    dxtcTab.removeAttr("onclick");
    dxtcTab[0].onclick = function (event) {
        contentWindow.ASPxPageControl1.ChangeActiveTab($(this).attr("id").replace("ASPxPageControl1_T", ""), false);
    };
    dxtcActiveTab.attr("id", "ASPxPageControl1_AT" + i);
    dxtcActiveTab.find("td").attr("id", "ASPxPageControl1_AT" + i + "T").text(p[0]);
    var color = dxtcActiveTab.css("border-bottom-color");
    var split = contentWindow.$('<td style="width:1px;border-bottom-color:' + color + ';border-bottom-style:Solid;border-bottom-width:1px;" id="ASPxPageControl1_T' + (i - 1) + 'S"><div style="height:1px;width:3px;overflow:hidden;"></div></td>');
    trHeader.find("td:last").before(split).before(dxtcTab).before(dxtcActiveTab);
    var iframeID = settings.moduleID.indexOf("Unified_") != -1 ? "iframeDataObject" + i : "iframeStdQuery" + i;
    contentWindow.$("#ASPxPageControl1_CC").append("<div style='display: none;' id='ASPxPageControl1_C" + i + "'>" +
    "<iframe id='" + iframeID + "' style='width: 100%;' frameborder='no' framespacing='0' border='0' src='" + settings.url + "'></iframe>"
     + "</div>");

    var tab = new contentWindow.ASPxClientTab(dxo, i, p[0]);
    pageControl.CreateTabProperties(tab, p);
    pageControl.tabs.push(tab);
    //ASPxPageControl1.tabCount = dxo.tabs.length;
    pageControl.SetActiveTab(tab);


    //    var isHave = $(this).find("#ASPxPageControl1_TC [moduleid='" + settings.moduleID + "']");


    //    var iframeWidth = $("#iframeContent").width();

    //    var tabTable = $(this).find("#ASPxPageControl1_TC > table");

    //    if (isHave.length == 1) {

    //        //开始，处理移动TAB标签
    //        var left = parseInt(tabTable.css("margin-left").replace("px", ""));
    //        var splitWidth = isHave.prev().width();
    //        var taboffset = isHave[0].offsetLeft + isHave[0].offsetWidth;
    //        if (iframeWidth < taboffset) {
    //            taboffset += splitWidth;
    //            var juli = iframeWidth - taboffset;
    //            if (parseInt(left) > juli) {
    //                tabTable.css("margin-left", juli + "px");
    //            }
    //        }
    //        else if (isHave[0].offsetLeft > left && tabTable.width() - iframeWidth > isHave[0].offsetLeft) {
    //            tabTable.css("margin-left", "-" + (isHave[0].offsetLeft - splitWidth) + "px");
    //        }
    //        //结束，处理移动TAB标签

    //        isHave.click();
    //        return false;
    //    }
    //    //tabTable.parent().width(iframeWidth);

    //    parent.parent.aspxLoadpanel.SetText("正在打开模块，请稍候。");
    //    aspxLoadpanel.Show();

    //    var headerTR = tabTable.children().children();
    //    var tabHeader = headerTR.children("td[class]");
    //    var moduleTab = $(this).find("#ASPxPageControl1_TC [moduleid]");

    //    var length = 1;
    //    if (moduleTab.length != 0) {
    //        length = parseInt(moduleTab.last().attr("id").replace("ASPxPageControl1_T", "")) + 1;
    //    }
    //    //增加Tab之间的间隙
    //    tabHeader.last().after("<td style=\"width:3px;border-bottom-color:#AECAF0;border-bottom-style:Solid;border-bottom-width:1px;\"><div style=\"height:1px;width:3px;overflow:hidden;\"></div></td>");

    //    //增加Tab
    //    var e1 = tabHeader.eq(0).clone().show().outerHTML();
    //    var e2 = tabHeader.eq(1).clone().hide().outerHTML();

    //    e1 = e1.replace("ASPxPageControl1_T0", "ASPxPageControl1_T" + length)
    //    e1 = e1.replace("ASPxPageControl1_T0T", "ASPxPageControl1_T" + length + "T");
    //    e1 = e1.replace("我的主页</td>", settings.text + "</td><td style='cursor:pointer;'><img height='10' style='margin-left: 3px;opacity: 0.5;' src='images/imgButton/close.png'/></td>");
    //    e1 = e1.replace("return aspxTCTClick(event, 'ASPxPageControl1', 0)", "return aspxTCTClick(event, 'ASPxPageControl1', " + length + ")");

    //    e2 = e2.replace("ASPxPageControl1_AT0", "ASPxPageControl1_AT" + length);
    //    e2 = e2.replace("我的主页</td>", settings.text + "</td><td style='cursor:pointer;'><img height='10' style='margin-left: 3px;opacity: 0.5;' src='images/imgButton/close.png'/></td>");
    //    e2 = e2.replace("ASPxPageControl1_AT0T", "ASPxPageControl1_AT" + length + "T");

    //    var e1Obj = $(e1);
    //    var e2Obj = $(e2);

    //    e1Obj.find("td:last img").closeTab(length);
    //    e2Obj.find("td:last img").closeTab(length);
    //    headerTR.children("td:last").prev().before(e1Obj).before(e2Obj);

    //    //开始，这是处理Default样式和其它样式兼容问题
    //    var tabhoverclass = "";
    //    tabhoverclass = e1Obj.attr("class").split("_")[1] == undefined ? "" : "_" + e1Obj.attr("class").split("_")[1];
    //    parent.$("#iframeContent")[0].contentWindow.aspxAddHoverItems('ASPxPageControl1', [[['dxtcTabHover' + tabhoverclass], [''], ['T' + length], ['', 'T'], [['']], ['Img']], [['dxtcActiveTabHover' + tabhoverclass], [''], ['AT' + length], ['', 'T'], [['']], ['Img']]]);
    //    //结束，这是处理Default样式和其它样式兼容问题

    //    var tabContent = $(this).find("tbody > tr:eq(1)").children("td:eq(0)");
    //    var iframeID = settings.moduleID.indexOf("Unified_") != -1 ? "iframeDataObject" + length : "iframeStdQuery" + length;
    //    tabContent.append(tabContent.children("div").height("100%").clone().html("<iframe id='" + iframeID + "' style='width: 100%; height: 100%;' frameborder='no' framespacing='0' border='0' src='" + settings.url + "'></iframe>").outerHTML().replace("ASPxPageControl1_C0", "ASPxPageControl1_C" + length)).css("overflow", "auto");

    //    headerTR.children("td[class]").last().prev().attr("moduleID", settings.moduleID).click();

    //    var iframe = tabContent.find("#" + iframeID)[0];
    //    if (iframe.attachEvent) {
    //        iframe.attachEvent("onload", function () {
    //            aspxLoadpanel.Hide();
    //            //alert("Local iframe is now loaded."); 
    //        });
    //    } else {
    //        iframe.onload = function () {
    //            aspxLoadpanel.Hide();
    //            //alert("Local iframe is now loaded."); 
    //        }
    //    }

    //    //    .onload = function () {
    //    //        aspxLoadpanel.Hide();
    //    //    }


    //    if (tabTable.width() > iframeWidth) {
    //        tabTable.parent().children("img").css("opacity", "0.3");
    //        tabTable.css("margin-left", ((iframeWidth - tabTable.width()) + "px"));
    //    }
    //    $.get("../HandelModule.ashx?action=open&ModuleID=" + settings.moduleID + "&ModuleName=" + settings.text);
}

$.fn.ResizeAll = function () {
    //    //修改navBar
        var height = $('#ASPxSplitter1_ASPxNavBar1').children().children().children().children().children().children().length * 27;
        $('#iframeMenu').height($('#ASPxSplitter1_1i1_CC').height() - height);
        //修改content
        $('#iframeContent')[0].contentWindow.ResizeIFrame($('#ASPxSplitter1_1i1_CC').height());
}

